package net.yurushao.fileop;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;

public class Assembler {

	/**
	 * Assemble little file to generate the original file
	 * @param path Path of little files, file name not included
	 * @param prefix Every little file have the same prefix
	 * @param name Name of the original file 
	 * @param num Number of files will be used to assemble a complete file
	 * @throws Exception
	 */
	public static void assembleFile(String path, String prefix, String name, int num) throws Exception {
		// TODO Auto-generated method stub
		byte[] buf = new byte[1024];
		
		File outFile = new File(path + "cp" + name);
		FileOutputStream fos = new FileOutputStream(outFile);
		
		for (int i = 1; i <= num; i++) {
			FileInputStream fis = new FileInputStream(
					new File(path + prefix + i + name));
			
			int read = -1; 
			while ((read = fis.read(buf)) != -1) {
				fos.write(buf, 0, read);
				fos.flush();
			}
			fis.close();
		}
		
		fos.close();
	}

}
